/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 *
 *
 */
package rad.framework.lang;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

/**
 * @author boudyacho
 * 
 */
public class IOUtil {

	/**
	 * @param is
	 * @param os
	 */
	public static void copy(InputStream is, OutputStream os) {
		// TODO is there a way to go straight to the db with the stream?
		try {
			int bytesRead;
			byte buffer[] = new byte[8192];
			while ((bytesRead = is.read(buffer)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
			os.flush();
			os.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static void writeFile(String name, String content) {
		try {
			FileOutputStream bos = new FileOutputStream(name);
			bos.write(content.getBytes());
			bos.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String readFile(String name) {
		FileInputStream in = null;
		InputStreamReader isr = null;
		try {
			in = new FileInputStream(name);
			isr = new InputStreamReader(in);
			StringBuffer buf = new StringBuffer();
			int ch = 0;
			while ((ch = in.read()) > -1) {
				buf.append((char) ch);
			}
			return buf.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			try {
				if (isr != null) {
					isr.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
	}

	public static void copyFile(String src, String tgt) {
		try {
			FileInputStream in = new FileInputStream(src);
			FileOutputStream out = new FileOutputStream(tgt);
			copy(in, out);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static void moveFile(String src, String tgt) {
		File file = new File(src);
		if (!file.renameTo(new File(tgt))) {
			throw new RuntimeException(src + " file not renamed");
		}
	}

	public static void deleteFile(String name) {
		File file = new File(name);
		if (!file.delete()) {
			throw new RuntimeException(name + " file not deleted");
		}
	}

	public static File[] listFiles(String name) {
		File file = new File(name);
		return file.listFiles();
	}
	
	public static String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		return sb.toString();
	}
}
